/**
 * 随机概率函数
 */
export function randomProbability<T>(arg: {
    id: T,
    /**
     * 请给他一个整数
     */
    weight: number
}[]) {
    if (arg.size() === 1) {
        return arg[0].id
    }
    /**
     * 随机总和
     */
    let total = 0;
    /**
     * 每个值的随机目标区间
     */
    const randomSeg: { min: number, max: number }[] = [];
    arg.forEach(c => {
        const nextTotal = total + c.weight
        randomSeg.push({
            min: total,
            max: nextTotal - 1
        })
        total = nextTotal;
    })
    const result = math.random(0, total - 1);
    let index = 0;
    for (const seg of randomSeg) {
        if (result >= seg.min && result <= seg.max) {
            break;
        }
        index++;
    }
    return arg[index].id
}
